package com.ledim.widget.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.letv.android.young.client.R;

/**
 * Created by qjq on 2016/11/30 15:05.
 */

public class LedimExpandableTextview extends FrameLayout implements View.OnClickListener {

    private Context mContext;
    private TextView expandableText;
    private TextView mTime;
    private ImageView mDown;
    private SparseBooleanArray mCollapsedStatus;
    private int mPosition;
    //速放的情况下，最大显示的行数
    private static final int MAX_LINE = 2;

    public LedimExpandableTextview(Context context) {
        super(context);
        this.mContext = context;
    }

    public LedimExpandableTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public LedimExpandableTextview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    private void init() {
        mCollapsedStatus = new SparseBooleanArray();
        View expandableTextView =
                View.inflate(mContext, R.layout.ledim_expandabletext_layout, null);
        expandableText = (TextView) expandableTextView.findViewById(R.id.tv_expandable_text);
        mTime = (TextView) expandableTextView.findViewById(R.id.tv_time);
        mDown = (ImageView) expandableTextView.findViewById(R.id.iv_down);
        mDown.setOnClickListener(this);
        addView(expandableTextView);

        final ViewTreeObserver mViewTreeObserver = this.getViewTreeObserver();
        mViewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public boolean onPreDraw() {
                expandableText.getViewTreeObserver().removeOnPreDrawListener(this);
                int lines = expandableText.getLineCount();
                if (lines > MAX_LINE) {
                    //显示
                    mDown.setVisibility(VISIBLE);
                } else {
                    mDown.setVisibility(GONE);
                }
                expandableText.setMaxLines(MAX_LINE);
                return true;
            }
        });
    }

    public void setData(final String desStr, int position, String createTime) {
        mPosition = position;
        mTime.setText(createTime);
        expandableText.setText(Html.fromHtml(desStr.trim()));
        if (TextUtils.isEmpty(desStr)) {
            mDown.setVisibility(GONE);
            expandableText.setVisibility(GONE);
        } else {
            mDown.setImageResource(R.drawable.contribute_down);
        }
    }

    @Override
    public void onClick(View v) {
        if (mDown.getVisibility() == VISIBLE) {
            if (mCollapsedStatus.get(mPosition)) {
                expandableText.setMaxLines(MAX_LINE);
                mCollapsedStatus.put(mPosition, false);
                mDown.setImageResource(R.drawable.contribute_down);
            } else {
                mCollapsedStatus.put(mPosition, true);
                expandableText.setMaxLines(Integer.MAX_VALUE);
                mDown.setImageResource(R.drawable.contribute_up);
            }
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }
}
